package ele.faultHandler;

import app.nbdevices.service.IFaultService;
import app.nbdevices.utils.ConvertUtil;
import app.service.DeviceManageService;
import app.service.FacilitiesManageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mess.warningmess.Bean4WarnMessInfo;
import com.mess.warningmess.WarningMass;
import ele.entity.EleCycleReport;
import ele.mapper.EleCycleReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import refactor.entity.Device;
import refactor.entity.Facilities;
import refactor.mapper.DeviceMapper;
import refactor.mapper.FacilitiesMapper;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;

/**
 * 电表故障处理
 */
@Component
public class EleFaultHandler {

    @Autowired
    @Qualifier("FaultService")
    IFaultService svcFault;

    @Autowired
    EleCycleReportMapper eleCycleReportMapper;

    @Autowired
    DeviceMapper deviceMapper;

    @Autowired
    FacilitiesMapper facilitiesMapper;

    /**
     * 主处理方法  用于将故障分发给次处理方法
     * @param eleCycleReport
     */
    public  void mainHandler(EleCycleReport eleCycleReport){
        byte[] res= ConvertUtil.getBase64Codec()
                .decode(eleCycleReport.getEleStatus());
        ArrayList<Integer> faultTypeIds=new ArrayList<>();
        //获取历史最新数据
        List<EleCycleReport> newRead = eleCycleReportMapper.getNewRead(eleCycleReport.getInternetDeviceId());
        EleCycleReport newEleCycleReport = newRead.get(0);

        //读数变小
        if(newEleCycleReport.getEleData()>eleCycleReport.getEleData()){
            clockPowerHandler(eleCycleReport.getInternetDeviceId());
            faultTypeIds.add(13);
        }
        //单日数据变化过大
        if((eleCycleReport.getEleData()-newEleCycleReport.getEleData())/(eleCycleReport.getDay()-newEleCycleReport.getDay())>60.00){
            dataBug(eleCycleReport.getInternetDeviceId());
            faultTypeIds.add(17);
        }
        //有功功率方向
        if(((res[0]>>2)&0x01)==1){
            clockPowerHandler(eleCycleReport.getInternetDeviceId());
            faultTypeIds.add(13);
        }
        //继电器状态
        if(((res[4]>>4)&0x01)==1){
            relayHandler(eleCycleReport.getInternetDeviceId());
            faultTypeIds.add(14);
        }
        svcFault.updateEventLog(eleCycleReport.getInternetDeviceId(),faultTypeIds,"1");
    }

    public void dropLine(){
        Long second = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        List<EleCycleReport> res=eleCycleReportMapper.selectNewData();
        for(EleCycleReport e:res){
//            if(second-e.getReportTime()>24*60*60*1000){
                if(second-e.getReportTime()>60*60*26){
                svcFault.saveEventLog(e.getInternetDeviceId(),7,12);
                }
        }
    }
   private void dataBug(String devId){
        svcFault.saveEventLog(devId,7,17);
    }
    private  void clockPowerHandler(String devId){
            svcFault.saveEventLog(devId,7,13);
    }

    private  void relayHandler(String devId){
            svcFault.saveEventLog(devId,7,14);
    }



    /**
     * 短信通知，由于未找到责任人，暂未开通
     * @param internetDeviceId
     */
    public void messAlter(String internetDeviceId){
        Device device=null;
        List<Device> devs = deviceMapper.selectList(new QueryWrapper<Device>().eq("internet_device_id", internetDeviceId));
        if(devs.size()>0){
            device = devs.get(0);
            List<Facilities> facs = facilitiesMapper.selectList(new QueryWrapper<Facilities>().eq("id", device.getFacilitiesId()));
            if(facs!=null){
                Facilities facilities = facs.get(0);
                String[] res=facilities.getAllocationType().split("/");
                Bean4WarnMessInfo info = new Bean4WarnMessInfo(device.getCode(), facilities.getTaskType(), facilities.getDescription(), "", res[0], res[1], " ");
                WarningMass.sendMess(info);
            }
        }
    }
    public static void main(String[] args) {
        for(int i=1;i<7;i++){
            for(int j=0;j<i;j++){
                if(j+i<7){
                    System.out.print(" ");
                }else{
                    System.out.print("&");
                }
            }
            System.out.println();
        }
       for(int i=0;i<78;i++){
           System.out.println();
           int a=0;
       }
     //   System.out.println("***********");
        for(int i=0;i<7;i++){
            for(int j=7;j>i;j--){
                if(i+j>7){
                    System.out.print(" ");
                }else{
                    System.out.print("&");
                }
            }
            System.out.println();
        }
    }
}
